package com.example.init.controller;

import com.example.init.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {
    @RequestMapping(value = "/m1")
    public String param(String name) {
        return "接收到的参数: "+name;
    }
    @RequestMapping("/m2")
    public String param(String name, Integer age) {
        return "接收到的参数: "+name +",age" + age;
    }

    @RequestMapping("/m3")
    public String param(Person person) {
        return "接收到的参数: "+person.toString();
    }

    @RequestMapping("/m4")
    public String m4(@RequestParam(value = "name",required = false) String username) {
        return "接收到的参数: "+username;
    }

    @RequestMapping("/m5")
    public String m5(String[] arrayName) {
        return "接收到的参数: "+ Arrays.toString(arrayName) + " 长度：" + arrayName.length;
    }

    @RequestMapping("/m6")
    public String m6(@RequestParam List<String> lists) {
        return "接收到的参数: "+ lists.toString() + " 长度：" + lists.size();
    }

    @RequestMapping("/m7")
    public String m7(@RequestBody Person person) {
        return "接收到的参数: "+ person.toString();
    }
    @RequestMapping("/m8/{userId}/{name}")
    public String m8(@PathVariable Integer userId,
                     @PathVariable("name") String userName) {
        return "接收到的参数: "+ userId +"   " + userName;
    }

    @RequestMapping("/m9")
    public String m9(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:/temp/"+file.getOriginalFilename()));
        return "success";
    }

}
